<template>
	<view class="container">
		<view class="goods-swiper">
			<swiper class="swiper" @change="handleGetSwiperIndex" :circular="true" :indicator-dots="false"
				:autoplay="true" interval="3500" duration="650">
				<swiper-item class="swiper-item" v-for="(item, index) in imageList" :key="items.id">
					<image :src="item" class="item-img" mode="scaleToFill" @click="pageShow(index)"></image>
				</swiper-item>
			</swiper>
			<view class="dots">
				<view class="item" v-for="item in imageList.length" :key="item" :class="{active:item === swiperNum}">
				</view>
			</view>
			<view class="change-icon">
				<image @click="handleReport" class="warning" src="@/static/goods/warning.png"></image>
				<button class="share-btn" open-type="share" :plain="true">
					<image class="share" src="@/static/goods/share.png"></image>
				</button>
			</view>
		</view>
		<view class="detail-card">
			<view class="draw">
				<image class="first" src="@/static/goods/first.png"></image>
				<view class="draw-btn" @click="showDiscount">
					领取
					<image class="draw-right" src="@/static/goods/chevron-right.png"></image>
				</view>
			</view>
			<view class="price">
				<view class="left-price">
					<view class="before">
						<text class="icon">￥</text>
						<text class="value">
							{{goodsInfo.dayRent_new ? goodsInfo.dayRent_new : goodsInfo.min_price}}
						</text>
						<text class="day">/天起</text>
					</view>
					<view class="after" v-if="goodsInfo.dayRent_new && goodsInfo.activity">
						券后价￥{{goodsInfo.min_price}}/天
					</view>
				</view>
				<view class="right-price">
					销量{{goodsInfo.sell_num}}
				</view>
			</view>
			<view class="goods-title">
				{{goodsInfo.extension_title}}
			</view>
			<view class="config">
				<view class="left-config">
					全新正品•全国包邮•可买断
				</view>
				<view class="right-config">
					365天起租
				</view>
			</view>
		</view>

		<view class="other">
			<view class="item item_send">
				<view class="title">租金</view>
				<view class="shop-price"> 市场价￥{{ goodsInfo.min_price_g }}起</view>
			</view>
			<view class="item item_send" style="margin-top: 10rpx;">
				<view class="title">配送</view>
				<view class="sended"> {{ goodsInfo.convey_type }}</view>
			</view>
		</view>

		<view class="goods-nav">
			<van-tabs @click="onChange">
				<van-tab title="商品详情">
					<view class="" style="padding-bottom: 100rpx;">
						<image mode="widthFix" :src="leaseDescImageInfo" class="info_img" />
						<rich-text class="rich-img" :nodes="goodsInfo.goods_content" style="font-size:0"></rich-text>
					</view>
				</van-tab>
				<van-tab title="租赁说明">
					<view class="" style="padding-bottom: 90rpx;">
						<view class="rent-img" v-for="(item, index) in rentImg" :key="index">
							<image :src="getImageUrl(item)" mode="widthFix"></image>
						</view>
					</view>
				</van-tab>
			</van-tabs>
		</view>

		<view class="goods-carts">
			<view class="home" @click="handleGoPage('index')">
				<image class="img" src="@/static/tabbar/home-dafault.png"></image>
				首页
			</view>
			<view class="service" @click="handleGoPage('service')">
				<image class="img" src="@/static/tabbar/service-default.png"></image>
				客服
			</view>
			<button class="btn" @click="shouMenu">免押租</button>
		</view>
		<!-- 打开优惠券 -->
		<van-popup :show="show" closeable="true" position="bottom"
			custom-style="height: 80%; border-radius: 30rpx 30rpx 0 0; padding: 30rpx 20rpx;" @close="closePopup">
			<view class="">优惠券领取</view>
			<my-discount :discountList="discountList"></my-discount>
		</van-popup>
		<goodMenu :goodsInfo="goodsInfo" ref="goodMenu"></goodMenu>
	</view>
</template>

<script>
	import goodMenu from '@/components/goodMenu/goodMenu.vue'
	export default {
		components: {goodMenu},
		data() {
			return {
				imageList: [], //轮播图
				active: 1,
				swiperNum: 0,
				show: false,
				id: null, //默认id
				rentImg: [
					'/material/info_phone_01.jpg',
					'/material/info_phone_02.jpg',
					'/material/info_phone_03.jpg',
					'/material/info_phone_04.jpg',
				],
				discountList:[],
				leaseDescImageInfo:getApp().globalData.baseImg+'material/alipay202207201409.jpg',
				platform_phone_icon:getApp().globalData.baseImg+'applet/service-Popup-icon3.png',
				options: [{
					icon: 'shop',
					text: '首页',
				}, {
					icon: 'chat',
					text: '客服'
				}],
				// 后端返回的商品信息
				goodsInfo: {},
				qrcode_url: "",
				bottomHeight: 0,
				isLoading: false,
			}
		},
		onLoad(options) {
			uni.showShareMenu({
				withShareTicket: true,
				menus: ["shareAppMessage", "shareTimeline"]
			})

			this.id = options.id //获取商品id
			this.$refs.goodMenu.tel = getApp().globalData.tel //获取手机号在组件中判断按钮
			this.getGoodsDetail() //获取id后发送请求获取详细数据

			
		},
		methods: {
			handleGoPage(path) {
				uni.switchTab({
					url: `/pages/${path}/${path}`,
				})
			},
			handleReport() {
				uni.navigateTo({
					url: "/pagesB/complaint/complaint",
				})
			},
			handleGetSwiperIndex(event) {
				this.swiperNum = event.detail.current;
			},
			getGoodsImg() {
				if (this.isLoading) return;
				this.isLoading = true;
				const data = {
					goods_id: this.goodsInfo.id,
					goods_name: this.goodsInfo.extension_title
				}
				uni.showLoading({
					title: "Loading",
					mask: true,
				})
				uni.$http.get('/Wechat/Order/toAlipay', data)
					.then(res => {
						if (res.data.status === 300) {
							setTimeout(() => {
								uni.switchTab({
									url: "/pages/my/my",
								})
							}, 3000)
							return uni.$showMsg('您还未登录，请先登录')
						}
						const {
							qrcode_url
						} = res.data.data;
						this.qrcode_url = qrcode_url;
					}).catch(() => {
						uni.removeStorageSync("tel");
						uni.removeStorageSync("token");
						uni.removeStorageSync("userInfo");
						uni.$showMsg('当前登录信息过期');
						setTimeout(() => {
							uni.switchTab({
								url: "/pages/my/my",
							})
						}, 2000)
					}).finally(() => {
						this.isLoading = false;
						uni.hideLoading();
					})
			},
			getImageUrl(img) {
				return getApp().globalData.baseImg + img;
			},
			//显示轮播图图片
			pageShow(index) {
				uni.previewImage({
					current: index,
					urls: this.imageList.map(item => item)
				})
			},
			// 请求商品详情
			getGoodsDetail() {
				uni.$http.get(`/Wechat/Goods/getGoodsInfo?goods_id=${this.id}`)
					.then(res => {
						const data = res.data.data;
						this.discountList=data.activity
						if (!data.carousel_images) {
							uni.redirectTo({
								url: `/pagesA/beOverdue/beOverdue`
							})
						}
						data.carousel_images = data.carousel_images.split(',');
						for (let i in data.carousel_images) {
							const path = getApp().globalData.baseImg + data.carousel_images[i];
							data.carousel_images[i] = path;
						}
						for (let i in data.pact_type) {
							if (data.pact_type[i] === "1") {
								data.pact_type[i] = "租完归还/续租/买断"
							} else if (data.pact_type[i] === "2") {
								data.pact_type[i] = "租完12期即送-租满6个月后支持随租随还"
							}
						}

						this.imageList = data.carousel_images //轮播图
						this.goodsInfo = data;
						this.goodsInfo.imgurl = getApp().globalData.baseImg + this.goodsInfo.imgurl //拼接图片

						// console.log(this.goodsInfo.goods_content, "this.goodsInfo.goods_content");	

						const reg = 'contenteditable="false"';
						const target = "style=\"width: 100%\"";
						let new_goods_content = "";
						if (this.goodsInfo.goods_content.indexOf(reg) !== -1) {
							new_goods_content = this.goodsInfo.goods_content.replaceAll(reg, target);
						} else {
							new_goods_content = this.goodsInfo.goods_content.replaceAll("<img",
								"<img style='width:100%'");
						}
						this.goodsInfo.goods_content = new_goods_content;

						this.$refs.goodMenu.imgurl = this.goodsInfo.imgurl
						this.$refs.goodMenu.id = this.goodsInfo.id
						// 颜色数组
						const attrData = this.goodsInfo.attr;
						this.findTypeData("color", 1);
						this.findTypeData("guige", 2);
						this.findTypeData("time", 3);
						this.findTypeData("unicom", 4);
						this.findTypeData("insurance", 5);
					})
			},
			findTypeData(name, type) {
				this.$refs.goodMenu[name] = this.goodsInfo.attr.filter(i => {
					return i.type === type
				})
			},
			//展示商品套餐
			shouMenu() {
				this.$refs.goodMenu.open()
			},
			// 展示优惠券
			showDiscount() {
				this.show = true
				console.log('=======')
			},
			closePopup() {
				this.show = false
			},
			// 切换标签栏
			onChange(event) {
				//uni.$showMsg(`切换到标签 ${event.detail.title}`);
			},
			// 点击左下角图标
			onClick(e) {
				if (e.content.text === '首页') {
					uni.switchTab({
						url: `/pages/index/index`
					})
				} else {
					uni.switchTab({
						url: "/pages/service/service"
					})
				}
			},

			// 加入购物车前的判断
			addCartFn(obj) {
				let {
					selectShop
				} = obj;
				// 模拟添加到购物车,请替换成你自己的添加到购物车逻辑
				let res = {};
				let name = selectShop.goods_name;
				if (selectShop.sku_name != "默认") {
					name += "-" + selectShop.sku_name;
				}
				res.msg = `${name} 已添加到购物车`;
				if (typeof obj.success == "function") obj.success(res);
			},
			// 加入购物车按钮
			addCart(selectShop) {
				console.log("监听 - 加入购物车");
				this.addCartFn({
					selectShop: selectShop,
					success: function(res) {
						// 实际业务时,请替换自己的加入购物车逻辑
						this.toast(res.msg);
					}
				});
			},

			toast(msg) {
				uni.showToast({
					title: msg,
					icon: "none"
				});
			}
		},
		onShareAppMessage() {
			return {
				title: this.goodsInfo.extension_title,
				path: `pagesA/category/goods/productDetails/productDetails?id=${this.goodsInfo.id}`,
				imageUrl: this.goodsInfo.imgurl
			}
		},
		onShareTimeline() {
			return {
				title: this.goodsInfo.extension_title + `---仅需${this.goodsInfo.min_price}元1天`,
				path: `pagesA/category/goods/productDetails/productDetails?id=${this.goodsInfo.id}`,
				imageUrl: this.goodsInfo.imgurl
			};
		},
	}
</script>

<style lang="scss" scoped>
	@import 'wxcomponents/vant-weapp/common/index.wxss';
	@import '@/wxcomponents/vant-weapp/tab/index.wxss';

	.container {
		width: 100vw;
		background-color: white;
	}
	.info_img{
		margin: 20px auto;
		display: block;
		width: 85%;
	}

	.goods-swiper {
		height: 500rpx;
		position: relative;

		.swiper {
			height: 500rpx;
			background-color: #fff;
			width: 100%;
			margin: 0 auto;

			.swiper-item {
				display: flex;
				align-items: center;
				justify-content: center;

				.item-img {
					width: 500rpx;
					height: 500rpx;
				}
			}
		}

		.dots {
			position: absolute;
			bottom: 20rpx;
			left: 50%;
			transform: translateX(-50%);
			display: flex;

			.item {
				width: 12rpx;
				height: 12rpx;
				border-radius: 16rpx;
				background-color: #C1C1C1;
				margin-right: 15rpx;
				transition: all .3s;

				&.active {
					width: 30rpx;
					background-color: #E43C3A;
				}
			}
		}

		.change-icon {
			position: absolute;
			right: 20rpx;
			top: 10rpx;
			display: flex;

			.warning,
			.share {
				width: 64rpx;
				height: 64rpx;
			}

			.share-btn {
				width: 64rpx;
				height: 64rpx;
				padding: 0;
				border: 0;
				margin-left: 17rpx;
				background-color: transparent;
			}
		}
	}


	.detail-card {
		width: 686rpx;
		padding: 24rpx;
		box-sizing: border-box;
		margin: 16rpx auto 0;
		background-color: white;
		box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(175, 175, 175, 0.25);
		border-radius: 16rpx;

		.draw {
			height: 60rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: linear-gradient(90deg, #FFF3F3 0%, rgba(254, 248, 247, 0.61) 100%);
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			padding: 0 16rpx;

			.first {
				width: 128rpx;
				height: 28rpx;
			}

			.draw-btn {
				display: flex;
				justify-content: space-between;
				align-items: center;
				color: #E43C3A;
				font-size: 20rpx;

				.draw-right {
					width: 28rpx;
					height: 28rpx;
				}
			}
		}

		.price {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 16rpx;
			
		
			.left-price {
				display: flex;
				color: #E43C3A;
				align-items: baseline;

				.before {
					display: flex;
					align-items: baseline;

					.icon {
						font-size: 16rpx;
					}

					.value {
						font-size: 48rpx;
					}

					.day {
						font-size: 16rpx;
					}
				}

				.after {
					margin-left: 16rpx;
					font-size: 24rpx;
					color: #999999;
					text-decoration: line-through;
				}
			}

			.right-price {
				font-size: 24rpx;
				color: #999999;
			}
		}

		.goods-title {
			font-size: 32rpx;
			color: #000000;
			margin-top: 24rpx;
			margin-bottom: 16rpx;
		}

		.config {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 24rpx;

			.left-config {
				color: #E43C3A;
			}

		}
	}


	.other {
		width: 686rpx;
		padding: 24rpx;
		box-sizing: border-box;
		margin: 16rpx auto 0;
		box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(175, 175, 175, 0.25);
		border-radius: 16rpx;
		.item_send{
			display: flex;
			justify-content: space-between;
		}
		.item {
			font-size: 24rpx;
			display: flex;
			align-items: center;
			height: 73rpx;
			line-height: 73rpx;
			.first-line {
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 100%;

				.sesame-box {
					display: flex;
					align-items: center;

					.sesame {
						width: 24rpx;
						height: 24rpx;
					}
				}
			}

			&:first-of-type {
				height: 75rpx;
				border-bottom: 2rpx solid #F3F3F3;
			}

			.title {
				color: #A6A6A6;
				margin-right: 16rpx;
				flex-shrink: 0;
			}
		}
	}

	.goods-carts {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100vw;
		z-index: 99;
		background-color: #fff;
		transition: all .7s;
		display: flex;
		align-items: center;
		padding-top: 20rpx;
		padding-bottom: 30rpx;

		.home,
		.service {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: 20rpx;
		}

		.home {
			margin-left: 96rpx;
		}

		.service {
			margin-left: 118rpx;
		}

		.img {
			width: 40rpx;
			height: 40rpx;
			margin-bottom: 16rpx;
		}

		.btn {
			z-index: 21;
			border-radius: 42rpx;
			color: #fff;
			font-size: 28rpx;
			width: 292rpx;
			height: 80rpx;
			line-height: 80rpx;
			margin-left: 116rpx;
			background: linear-gradient(90deg, #FB594C 0%, #F93134 100%);
		}
	}



	.rich-img img {
		width: 100% !important;
	}

	.rent-img image {
		display: block;
		width: 100%;
		padding-bottom: 0 !important;
	}

	.goods-nav {
		margin-top: 32rpx;
		padding-bottom: 50rpx;
	}
</style>
